package com.esd.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.esd.crm.Constants;
import com.esd.crm.entity.Address;
import com.esd.crm.entity.Customer;
import com.esd.crm.entity.District;
import com.esd.crm.service.AddressService;
import com.esd.crm.service.CustomerService;
import com.esd.crm.service.DistrictService;

@Controller
@RequestMapping("/security")
public class AddressController {
	private static final Logger logger = LogManager.getLogger(AddressController.class);

	@Autowired
	private AddressService addressService;
	@Autowired
	private DistrictService districtService;

	@Autowired
	private CustomerService customerService;

	/**
	 * 跳转更新订单页
	 */
	@RequestMapping(value = "/updateAddress", method = RequestMethod.GET)
	public ModelAndView updateAddressGet(HttpSession session, HttpServletRequest request) {
		logger.info("跳转到更新订单页");
		ModelAndView mv = new ModelAndView("order/order_update");
		return mv;
	}

	/**
	 * 更新订单页
	 */
	@RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
	public ModelAndView updateAddressPost(HttpSession session, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		logger.info("更新地址页面");
		Object userId = session.getAttribute(Constants.USER_ID);
		
		String id = request.getParameter("id");
		Address record = addressService.selectByPrimaryKey(Integer.valueOf(id));
		Integer updateuser = Integer.valueOf(userId.toString());
		record.setUpdatetime(new Date());
		record.setUpdateuser(updateuser);
		addressService.updateByPrimaryKeySelective(record);
		return new ModelAndView("redirect:/security/success");
	}

	/**
	 * 跳转添加订单页
	 */
	@RequestMapping(value = "/addAddress", method = RequestMethod.GET)
	public ModelAndView addAddressGet(HttpSession session, HttpServletRequest request) {
		logger.info("跳转到常用地址页面");
		ModelAndView mv = new ModelAndView("address/address_add");
		String customerId = request.getParameter("customerId");
		// String customerId = "28";
		if (StringUtils.isNotBlank(customerId)) {
			Customer c = customerService.selectByPrimaryKey(Integer.valueOf(customerId));
			mv.addObject("customerId", c.getId());
			return mv;
		}
		return new ModelAndView("address/address_add");
	}

	/**
	 * 跳转添加订单页
	 */
	@RequestMapping(value = "/addAddress", method = RequestMethod.POST)
	public ModelAndView addAddressPost(HttpSession session, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		logger.info("添加订单页");
		Object userId = session.getAttribute(Constants.USER_ID);
		Integer createuser = Integer.valueOf(userId.toString());

		String customerid = request.getParameter("customerId");
		if(StringUtils.isBlank(customerid)){
			
		}
		String shi = request.getParameter("shi");
		String quxian = request.getParameter("quxian");

		District shi_d = districtService.selectByPrimaryKey(Integer.valueOf(shi));
		District quxian_d = districtService.selectByPrimaryKey(Integer.valueOf(quxian));
		String address = shi_d.getAreaname() + quxian_d.getAreaname();

		String detailedAddress = request.getParameter("detailedAddress");
		String name = request.getParameter("name");
		String telephone = request.getParameter("telephone");

		Address a = new Address();
		a.setAddress(address);
		a.setCustomerid(Integer.valueOf(customerid));
		a.setDetailedAddress(detailedAddress);
		a.setName(name);
		a.setTelephone(telephone);
		a.setCreateuser(createuser);

		addressService.insertSelective(a);

		redirectAttributes.addAttribute("customerId", customerid);
		//return new ModelAndView("redirect:/security/order");
		return new ModelAndView("redirect:/security/successAndClose");
	}

	
	
	@RequestMapping(value = "/getAddressText", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAddressText(HttpSession session, HttpServletRequest request) {
		logger.info("获取地址文本");
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotBlank(id)) {
			Address a = addressService.selectByPrimaryKey(Integer.valueOf(id));
			StringBuffer sb = new StringBuffer();
			sb.append(a.getAddress()).append(" ");
			sb.append(a.getDetailedAddress()).append(" ");
			sb.append(a.getName()).append(" ");
			sb.append(a.getTelephone());
			logger.info(sb);
			map.put("t", sb.toString());
		}
		return map;
	}
	
	
	/**
	 * 跳转添加产品页
	 */
	@RequestMapping(value = "/address", method = RequestMethod.GET)
	public ModelAndView orderGet(HttpSession session, HttpServletRequest request) {
		logger.info("跳转到常用地址查询页");
		ModelAndView mv = new ModelAndView("order/order");
		String customerId = request.getParameter("customerId");
		if (StringUtils.isNotBlank(customerId)) {
			// Customer c = customerService.selectByPrimaryKey(Integer.valueOf(customerId));
			// mv.addObject("customerId", c.getId());
			// mv.addObject("name", c.getName());
			// mv.addObject("card", c.getCard());
			// mv.addObject("orderNumber", CalendarUtil.getOrderNumber());
		}
		return mv;
	}

	@RequestMapping(value = "/address", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addressPost(HttpSession session, HttpServletRequest request) {
		logger.info("常用地址查询");
		Map<String, Object> map = new HashMap<String, Object>();
		Integer customerId = null;
		String id = request.getParameter("customerId");
		if (StringUtils.isNotBlank(id)) {
			customerId = Integer.valueOf(id);
		}
		int totle = 1;
		List<Address> list = null;
		// 组合查询参数
		int page = Integer.valueOf(request.getParameter("page"));
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("customerId", customerId);
		param.put("page", (page - 1) * 10);
		param.put("totle", 10);
		list = addressService.selectByParam(param);
		totle = addressService.selectByParamCount(param);
		int u = totle % 10;
		int pageCount = 1;
		if (u > 0) {
			pageCount = totle / 10 + 1;
		} else {
			pageCount = totle / 10;
		}
		map.put(Constants.LIST, list);
		map.put(Constants.TOTLE, totle);
		map.put(Constants.TOTLE_PAGE, pageCount);
		return map;
	}

	@RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAddress(HttpSession session, HttpServletRequest request) {
		logger.info("删除常用地址");
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		addressService.deleteByPrimaryKey(Integer.valueOf(id));
		map.put("message", "删除成功");
		return map;
	}
	
	@RequestMapping(value = "/setDefAddress", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> setDefAddress(HttpSession session, HttpServletRequest request) {
		logger.info("设置常用地址");
		Map<String, Object> map = new HashMap<String, Object>();
		String customerId = request.getParameter("customerId");
		String id = request.getParameter("id");
		Address record = new Address();
		record.setCustomerid(Integer.valueOf(customerId));
		addressService.updateFrequently(record);
		Address f =addressService.selectByPrimaryKey(Integer.valueOf(id));
		f.setFrequently(true);
		addressService.updateByPrimaryKeySelective(f);
		map.put("message", "设置成功");
		return map;
	}

}
